package com.zzl.spark.sql;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SQLContext;

public class DataFrameOpsFromFile {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf().setMaster("local").setAppName("DataFrameOps");
        JavaSparkContext sc = new JavaSparkContext(conf);
        System.setProperty("hadoop.home.dir", "E:\\hadoop");
        sc.setLogLevel("WARN");

        SQLContext sqlContext =new SQLContext(sc); //new SQLContext(sc);
//        Dataset<Row> df =sqlContext.read().format("json").load("people.json");
        Dataset<Row> df = sqlContext.read().json("people.json");
        df.show();

        df.printSchema();

        df.registerTempTable("tb");

        String SQLtext = "select age,count(0) as countAge from tb group by age";

        Dataset<Row> sql = sqlContext.sql(SQLtext);
        sql.show();
//        df.select("age").show();
//
//        df.select(df.col("name"),df.col("age").plus(10).as("plusAge")).show();
//
//        df.filter(df.col("age").gt(19)).show();
        sc.stop();
    }
}
